<template>
  <div class="health">
    <!-- 基本柱状图 -->
    <div class="one">
      <p>基本柱状图</p>
      <div id="health-one"></div>
    </div>
    <!-- 堆叠柱状图 -->
    <div class="two">
      <p>堆叠柱状图</p>
      <div id="health-two"></div>
    </div>
    <!-- (平均值,最大(小)值柱状图) -->
    <div class="three">
      <p>(平均值,最大(小)值柱状图)</p>
      <div id="health-three"></div>
    </div>
    <div class="four">
      <p>柱状图动画延迟</p>
      <div id="health-four"></div>
    </div>
  </div>
</template>

<script>
import * as echarts from 'echarts';
export default {
  data(){
    return {}
  },
  mounted(){
    // 基本柱状图
    this.getOne()
    // 堆叠柱状图
    this.getTwo()
    // (平均值,最大(小)值柱状图)
    this.getThree()
    // 柱状图动画延迟
    this.getFour()
  },
  methods: {
    // 基本柱状图
    getOne(){
      var chartDom = document.getElementById('health-one');
      var myChart = echarts.init(chartDom);
      var option;

      option = {
        xAxis: {
          type: 'category',
          data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            data: [120, 200, 150, 80, 70, 110, 130],
            type: 'bar'
          }
        ]
      };

      option && myChart.setOption(option);
    },
    // 堆叠柱状图
    getTwo(){
      var chartDom = document.getElementById('health-two');
      var myChart = echarts.init(chartDom);
      var option;

      option = {
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        legend: {},
        grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        xAxis: [
          {
            type: 'category',
            data: ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        series: [
          {
            name: 'Direct',
            type: 'bar',
            emphasis: {
              focus: 'series'
            },
            data: [320, 332, 301, 334, 390, 330, 320]
          },
          {
            name: 'Email',
            type: 'bar',
            stack: 'Ad',
            emphasis: {
              focus: 'series'
            },
            data: [120, 132, 101, 134, 90, 230, 210]
          },
          {
            name: 'Union Ads',
            type: 'bar',
            stack: 'Ad',
            emphasis: {
              focus: 'series'
            },
            data: [220, 182, 191, 234, 290, 330, 310]
          },
          {
            name: 'Video Ads',
            type: 'bar',
            stack: 'Ad',
            emphasis: {
              focus: 'series'
            },
            data: [150, 232, 201, 154, 190, 330, 410]
          },
          {
            name: 'Search Engine',
            type: 'bar',
            data: [862, 1018, 964, 1026, 1679, 1600, 1570],
            emphasis: {
              focus: 'series'
            },
            markLine: {
              lineStyle: {
                type: 'dashed'
              },
              data: [[{ type: 'min' }, { type: 'max' }]]
            }
          },
          {
            name: 'Baidu',
            type: 'bar',
            barWidth: 5,
            stack: 'Search Engine',
            emphasis: {
              focus: 'series'
            },
            data: [620, 732, 701, 734, 1090, 1130, 1120]
          },
          {
            name: 'Google',
            type: 'bar',
            stack: 'Search Engine',
            emphasis: {
              focus: 'series'
            },
            data: [120, 132, 101, 134, 290, 230, 220]
          },
          {
            name: 'Bing',
            type: 'bar',
            stack: 'Search Engine',
            emphasis: {
              focus: 'series'
            },
            data: [60, 72, 71, 74, 190, 130, 110]
          },
          {
            name: 'Others',
            type: 'bar',
            stack: 'Search Engine',
            emphasis: {
              focus: 'series'
            },
            data: [62, 82, 91, 84, 109, 110, 120]
          }
        ]
      };

      option && myChart.setOption(option);
    },
    // (平均值,最大(小)值柱状图)
    getThree(){
      var chartDom = document.getElementById('health-three');
      var myChart = echarts.init(chartDom);
      var option;

      option = {
        title: {
          text: 'Rainfall vs Evaporation',
          subtext: 'Fake Data'
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['Rainfall', 'Evaporation']
        },
        toolbox: {
          show: true,
          feature: {
            dataView: { show: true, readOnly: false },
            magicType: { show: true, type: ['line', 'bar'] },
            restore: { show: true },
            saveAsImage: { show: true }
          }
        },
        calculable: true,
        xAxis: [
          {
            type: 'category',
            // prettier-ignore
            data: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
          }
        ],
        yAxis: [
          {
            type: 'value'
          }
        ],
        series: [
          {
            name: 'Rainfall',
            type: 'bar',
            data: [
              2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3
            ],
            markPoint: {
              data: [
                { type: 'max', name: 'Max' },
                { type: 'min', name: 'Min' }
              ]
            },
            markLine: {
              data: [{ type: 'average', name: 'Avg' }]
            }
          },
          {
            name: 'Evaporation',
            type: 'bar',
            data: [
              2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3
            ],
            markPoint: {
              data: [
                { name: 'Max', value: 182.2, xAxis: 7, yAxis: 183 },
                { name: 'Min', value: 2.3, xAxis: 11, yAxis: 3 }
              ]
            },
            markLine: {
              data: [{ type: 'average', name: 'Avg' }]
            }
          }
        ]
      };

      option && myChart.setOption(option);
    },
    // 柱状图动画延迟
    getFour(){
      var chartDom = document.getElementById('health-four');
      var myChart = echarts.init(chartDom);
      var option;

      var xAxisData = [];
      var data1 = [];
      var data2 = [];
      for (var i = 0; i < 100; i++) {
        xAxisData.push('A' + i);
        data1.push((Math.sin(i / 5) * (i / 5 - 10) + i / 6) * 5);
        data2.push((Math.cos(i / 5) * (i / 5 - 10) + i / 6) * 5);
      }
      option = {
        title: {
          text: 'Bar Animation Delay'
        },
        legend: {
          data: ['bar', 'bar2']
        },
        toolbox: {
          // y: 'bottom',
          feature: {
            magicType: {
              type: ['stack']
            },
            dataView: {},
            saveAsImage: {
              pixelRatio: 2
            }
          }
        },
        tooltip: {},
        xAxis: {
          data: xAxisData,
          splitLine: {
            show: false
          }
        },
        yAxis: {},
        series: [
          {
            name: 'bar',
            type: 'bar',
            data: data1,
            emphasis: {
              focus: 'series'
            },
            animationDelay: function (idx) {
              return idx * 10;
            }
          },
          {
            name: 'bar2',
            type: 'bar',
            data: data2,
            emphasis: {
              focus: 'series'
            },
            animationDelay: function (idx) {
              return idx * 10 + 100;
            }
          }
        ],
        animationEasing: 'elasticOut',
        animationDelayUpdate: function (idx) {
          return idx * 5;
        }
      };

      option && myChart.setOption(option);
    }
  }
}
</script>

<style lang="scss" scoped>
p {
  height: 50px;
  width: 100%;
  line-height: 50px;
  text-align: center;
  border-bottom: 1px solid #ccc;
  font-weight: 700;
  box-sizing: border-box;
}
.health {
  width: 100%;
  height: auto;
  // 基础柱状图
  .one {
    border: 1px solid #ccc;
    box-sizing: border-box;
    #health-one {
      height: 400px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
  // 堆叠柱状图
  .two {
    border: 1px solid #ccc;
    box-sizing: border-box;
    #health-two {
      height: 400px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
  // (平均值,最大(小)值柱状图)
  .three {
    border: 1px solid #ccc;
    box-sizing: border-box;
    #health-three {
      height: 400px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
  // 柱状图动画延迟
  .four {
    border: 1px solid #ccc;
    box-sizing: border-box;
    #health-four {
      height: 400px;
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
}
</style>